package com.github.zhijieqing.socket.cluster.starter;

import com.github.zhijieqing.common.web.utils.EnvUtil;
import com.github.zhijieqing.common.web.utils.SpringContextUtils;
import com.github.zhijieqing.socket.cluster.common.channel.MsgChannel;
import com.github.zhijieqing.socket.cluster.common.config.SocketClusterConfig;
import com.github.zhijieqing.socket.cluster.common.manager.DefaultSocketManager;
import com.github.zhijieqing.socket.cluster.common.manager.SocketManager;
import com.github.zhijieqing.socket.cluster.common.node.SocketServerNode;
import com.github.zhijieqing.socket.cluster.common.storage.ClientStorage;
import com.github.zhijieqing.socket.cluster.redis.RedisClientStorage;
import com.github.zhijieqing.socket.cluster.redis.RedisMsgChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.UUID;

@Slf4j
@Configuration
public class SocketClusterAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SocketManager socketManager(SocketClusterConfig socketClusterConfig, ClientStorage clientStorage, MsgChannel msgChannel){
        buildServerNode(socketClusterConfig);
        SocketManager socketManager=new DefaultSocketManager();
        socketManager.setSocketClusterConfig(socketClusterConfig);
        socketManager.setClientStorage(clientStorage);
        socketManager.setMsgChannel(msgChannel);
        socketManager.init();
        log.info("create socketManager success");
        return socketManager;
    }

    private void buildServerNode(SocketClusterConfig socketClusterConfig){
        if (socketClusterConfig.getServerNode() == null) {
            socketClusterConfig.setServerNode(new SocketServerNode());
        }
        if (StringUtils.isEmpty(socketClusterConfig.getServerNode().getId())) {
            socketClusterConfig.getServerNode().setId(UUID.randomUUID().toString());
        }
        if (StringUtils.isEmpty(socketClusterConfig.getServerNode().getName())) {
            socketClusterConfig.getServerNode().setName(EnvUtil.getEvn().getProperty("spring.application.name"));
        }
        if (StringUtils.isEmpty(socketClusterConfig.getServerNode().getIp())) {
            try {
                InetAddress ip4 = Inet4Address.getLocalHost();
                socketClusterConfig.getServerNode().setIp(ip4.getHostAddress());
            } catch (Exception e) {
                log.error("获取不到IP", e);
            }
        }
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties("socket.cluster")
    public SocketClusterConfig socketClusterConfig(){
        SocketClusterConfig config=new SocketClusterConfig();
        return config;
    }

    @Bean
    @ConditionalOnMissingBean
    public ClientStorage clientStorage(RedissonClient redissonClient){
        RedisClientStorage redisClientStorage=new RedisClientStorage();
        redisClientStorage.setRedissonClient(redissonClient);
        return redisClientStorage;
    }

    @Bean
    @ConditionalOnMissingBean
    public MsgChannel msgChannel(RedissonClient redissonClient){
        RedisMsgChannel redisMsgChannel=new RedisMsgChannel();
        redisMsgChannel.setRedissonClient(redissonClient);
        return redisMsgChannel;
    }
}
